package com.live.shop.tool;

import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

public class AccessibilityUtils {

    public static void printNodeInfo(AccessibilityNodeInfo node) {
        printNodeInfo(node, 0,"", false);
    }

    private static void printNodeInfo(AccessibilityNodeInfo node, int indexNum ,String prefix, boolean isLast) {
        if (node == null) {
            return;
        }
        // |  |  |  |  |  \--- NodeWrapper{text='发芽的米老：牛',
        NodeWrapper nodeWrapper = new NodeWrapper(
                node.getText() != null ? node.getText().toString() : "",
                node.getViewIdResourceName() != null ? node.getViewIdResourceName() : "",
                node.getClassName() != null ? node.getClassName().toString() : "");

        String marker = isLast ? "[最后]" : "[检索]";
        String currentPrefix = prefix + marker;
        int size = node.getChildCount();
//        if(node.getText() != null && node.getText().toString().contains("：")) Log.d("printNodeInfo", currentPrefix + nodeWrapper.toString()+"第【"+indexNum+"】个，该Node有【"+size+"】子Node,");
        Log.d("printNodeInfo", currentPrefix + nodeWrapper.toString()+"第【"+indexNum+"】个，该Node有【"+size+"】子Node,");
        if (size > 0) {
            String childPrefix = prefix + (isLast ? "  " : "|  ");
            int lastChildIndex = size - 1;
            for (int index = 0; index < size; index++) {
                boolean isLastChild = index == lastChildIndex;
                printNodeInfo(node.getChild(index),index, childPrefix, isLastChild);
            }
        }
    }

    private static class NodeWrapper {
        private String text;
        private String id;
        private String className;

        public NodeWrapper(String text, String id, String className) {
            this.text = text;
            this.id = id;
            this.className = className;
        }
        @Override
        public String toString() {
            return "NodeWrapper{" +
                    "text='" + text + '\'' +
                    ", id='" + id + '\'' +
                    ", className='" + className + '\'' +
                    '}';
        }
    }
}